package com.jzo2o.customer.service.impl;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.CurrentUser;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.UserContext;
import com.jzo2o.customer.model.domain.AgencyCertificationAudit;
import com.jzo2o.customer.model.dto.AgencyCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.service.AgencyCertificationAuditService;
import com.jzo2o.customer.mapper.AgencyCertificationAuditMapper;
import com.jzo2o.customer.service.IAgencyCertificationService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author admin
 * @description 针对表【agency_certification_audit(机构认证审核表)】的数据库操作Service实现
 * @createDate 2025-11-06 21:54:34
 */
@Service
public class AgencyCertificationAuditServiceImpl extends ServiceImpl<AgencyCertificationAuditMapper, AgencyCertificationAudit>
        implements AgencyCertificationAuditService {
    @Autowired
    private IAgencyCertificationService agencyCertificationService;
    @Autowired
    private AgencyCertificationAuditService agencyCertificationAuditService;
    @Override
    public PageResult<AgencyCertificationAudit> pageQueryWorker(AgencyCertificationAuditPageQueryReqDTO agencyDTO) {
        return PageHelperUtils.selectPage(agencyDTO, () -> {
            return lambdaQuery()
                    .eq(ObjectUtil.isNotEmpty(agencyDTO.getCertificationStatus()), AgencyCertificationAudit::getCertificationStatus, agencyDTO.getCertificationStatus())
                    .eq(StrUtil.isNotEmpty(agencyDTO.getLegalPersonName()), AgencyCertificationAudit::getLegalPersonName, agencyDTO.getLegalPersonName())
                    .eq(StrUtil.isNotEmpty(agencyDTO.getName()), AgencyCertificationAudit::getName, agencyDTO.getName())
                    .list();
        });
    }

    @Override
    public void audit(Long id, AgencyCertificationAudit agencyCertificationAudit) {
        // 更新机构认证状态
        AgencyCertificationAudit agencyCertificationAudit1 = agencyCertificationAuditService.getById(id);
        AgencyCertificationUpdateDTO updateDTO = AgencyCertificationUpdateDTO.builder()
                                                                             .id(agencyCertificationAudit1.getServeProviderId())
                                                                             .certificationStatus(agencyCertificationAudit.getCertificationStatus())
                                                                             .certificationTime(LocalDateTime.now())
                                                                             .build();
        agencyCertificationService.updateByServeProviderId(updateDTO);
        // 写审核表
        CurrentUser user = UserContext.getCurrentUser();
        Long auditorId = user == null ? null : user.getId();
        String auditorName = user == null ? null : user.getName();

        agencyCertificationAudit1.setAuditStatus(1);
        agencyCertificationAudit1.setCertificationStatus(agencyCertificationAudit.getCertificationStatus());
        agencyCertificationAudit1.setAuditorId(auditorId);
        agencyCertificationAudit1.setAuditorName(auditorName);
        agencyCertificationAudit1.setAuditTime(LocalDateTime.now());

        agencyCertificationAuditService.updateById(agencyCertificationAudit1);
    }
}
